/**
 * 作者: hxl
 * 日期: 2024-12-11 14:58:44
 * 备注: 用户花店表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserFlorist;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserFloristModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // 玩家id
    protected String task = ""; // 任务列表
    protected String secretaries = ""; // 可获取的秘书列表
    protected String secretaryUnlock = ""; // 已解锁的秘书列表

    public void reset() {
    }

    protected String getKey() {
        return "UserFlorist." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_florist"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 玩家id
        obj.put("task", task); // 任务列表
        obj.put("secretaries", secretaries); // 可获取的秘书列表
        obj.put("secretaryUnlock", secretaryUnlock); // 已解锁的秘书列表
        return obj;
    }

    public PBUserFlorist.Builder takeInitBuilder() {
        PBUserFlorist.Builder sb = PBUserFlorist.newBuilder();
        if (id != 0) {
            sb.setId(id);
        }
        if (this.task != null && this.task.length() > 0) {
            JsonArray list = new JsonArray(this.task);
            for(int i=0; i<list.size(); i++) {
                sb.addTask(list.getInteger(i));
            }
        }
        if (this.secretaries != null && this.secretaries.length() > 0) {
            JsonArray list = new JsonArray(this.secretaries);
            for(int i=0; i<list.size(); i++) {
                sb.addSecretaries(list.getInteger(i));
            }
        }
        if (this.secretaryUnlock != null && this.secretaryUnlock.length() > 0) {
            JsonArray list = new JsonArray(this.secretaryUnlock);
            for(int i=0; i<list.size(); i++) {
                sb.addSecretaryUnlock(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserFlorist.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserFlorist.Builder sb = PBUserFlorist.newBuilder();
        if (isFieldUpdate("id")) {
            sb.setId(id);
        }
        if (isFieldUpdate("task")) {
            if (this.task != null && this.task.length() > 0) {
                JsonArray list = new JsonArray(this.task);
                for(int i=0; i<list.size(); i++) {
                    sb.addTask(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearTask();
                }
            } else {
                sb.clearTask();
            }
        }
        if (isFieldUpdate("secretaries")) {
            if (this.secretaries != null && this.secretaries.length() > 0) {
                JsonArray list = new JsonArray(this.secretaries);
                for(int i=0; i<list.size(); i++) {
                    sb.addSecretaries(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearSecretaries();
                }
            } else {
                sb.clearSecretaries();
            }
        }
        if (isFieldUpdate("secretaryUnlock")) {
            if (this.secretaryUnlock != null && this.secretaryUnlock.length() > 0) {
                JsonArray list = new JsonArray(this.secretaryUnlock);
                for(int i=0; i<list.size(); i++) {
                    sb.addSecretaryUnlock(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearSecretaryUnlock();
                }
            } else {
                sb.clearSecretaryUnlock();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public JsonArray getTaskArray() {
        if (this.task != null && this.task.length() > 0) {
            return new JsonArray(this.task);
        } else {
            return new JsonArray();
        }
    }

    public String getTask() {
        return task;
    }

    public void putTask(String task) {
        if (!this.task.equals(task)) {
            this.task = task;
            updateRedisHashField(getKey(), "task", this.task);
        }
    }

    public JsonArray getSecretariesArray() {
        if (this.secretaries != null && this.secretaries.length() > 0) {
            return new JsonArray(this.secretaries);
        } else {
            return new JsonArray();
        }
    }

    public String getSecretaries() {
        return secretaries;
    }

    public void putSecretaries(String secretaries) {
        if (!this.secretaries.equals(secretaries)) {
            this.secretaries = secretaries;
            updateRedisHashField(getKey(), "secretaries", this.secretaries);
        }
    }

    public JsonArray getSecretaryUnlockArray() {
        if (this.secretaryUnlock != null && this.secretaryUnlock.length() > 0) {
            return new JsonArray(this.secretaryUnlock);
        } else {
            return new JsonArray();
        }
    }

    public String getSecretaryUnlock() {
        return secretaryUnlock;
    }

    public void putSecretaryUnlock(String secretaryUnlock) {
        if (!this.secretaryUnlock.equals(secretaryUnlock)) {
            this.secretaryUnlock = secretaryUnlock;
            updateRedisHashField(getKey(), "secretaryUnlock", this.secretaryUnlock);
        }
    }

}